package wj.tree;

import java.util.ArrayList;
import java.util.HashMap;

import wj.Function;
import wj.Scope;
import wj.WJParser;
import wj.WJValue;

public class FunctionCallNode implements WJNode{

	private String name;
	private ArrayList<WJNode> arguments;
	
	public FunctionCallNode(String name, ArrayList<WJNode> arguments) {
		this.name = name;
		if(arguments == null)
			arguments = new ArrayList<WJNode>();
		this.arguments = arguments;
	}


	@Override
	public WJValue evaluate() {
		Function target = WJParser.functions.get(name+"-"+arguments.size());
		
		ArrayList<WJValue> argValues = new ArrayList<WJValue>();
		for(WJNode arg: arguments)
			argValues.add(arg.evaluate());
		
		if(target == null)
			throw new RuntimeException("Tried to call function named "+name+" with "+arguments.size()+" parameters but it doesn't exist.");
		
		return target.execute(argValues, WJParser.functions);
	}

}
